package xml;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author seetharamanj
 */
public class EditXml
{
    private DocumentBuilderFactory docFactory;
    private DocumentBuilder docBuilder;
    private Document doc;
    private String fileName;
    private File fileIn;
    public EditXml(String fName)throws Exception
    {
        fileName=fName;
        fileIn=new File(fileName);
        docFactory = DocumentBuilderFactory.newInstance();
        docBuilder = docFactory.newDocumentBuilder();
        doc = docBuilder.parse(fileName);
    }
    public void writeToFile()throws Exception
    {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StreamResult result = new StreamResult(new File("xmlOut.xml"));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    }
    public void setAttrib(String tagName, String attribName, String newValue)
    {        
        NodeList editNodes=doc.getElementsByTagName(tagName);     
        Element editNode=(Element)editNodes.item(0);
        editNode.setAttribute(attribName,newValue);
    }

    public void addChild(String parent,String child, String value)
    {
        Node newTag = doc.createElement(child);
        newTag.setTextContent(value);
        NodeList nodeList=doc.getElementsByTagName(parent);
        Node after=nodeList.item(0);
        after.appendChild(newTag);
    }

    public void setValue(String tagName, String newValue)
    {
        NodeList editNodes=doc.getElementsByTagName(tagName);
        Node editNode=editNodes.item(0);
        editNode.setTextContent(newValue);
    }

    public void setChildValue(String parentTag, String childTag, String newValue)
    {
        NodeList parentNode=doc.getElementsByTagName(parentTag);
        Element editNode=(Element)parentNode.item(0);
        NodeList childNodes=editNode.getElementsByTagName(childTag);
        Node child=childNodes.item(0);
        child.setTextContent(newValue);
    }
    public void setIfChildValue(String parentTag, String ifTag, String ifVal, String childTag, String newValue)
    {
        NodeList parentNode=doc.getElementsByTagName(parentTag);
        int nodeCount=parentNode.getLength();
        for(int i=0;i<nodeCount;i++)
        {
            Element editNode=(Element)parentNode.item(i);
            NodeList ifTags=editNode.getElementsByTagName(ifTag);
            Node ifTg=ifTags.item(0);
            if(ifTg.getTextContent().equals(ifVal))
            {
                NodeList childNodes=editNode.getElementsByTagName(childTag);
                Node child=childNodes.item(0);
                child.setTextContent(newValue);
            }
        }
    }
}
